package com.wiim.ui;

import com.wiim.common.Utilities;
import com.wiim.nhapnhap.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.app.Activity;

public class QRCodeFrament extends Fragment {

	public static final int REQUEST_CODE_QR = 0x0000c0de;
	
	public static QRCodeFrament qrFrag;
	
//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		// TODO Auto-generated method stub
//		super.onCreate(savedInstanceState);
////		setContentView(R.layout.mainlocation);
//	}

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.mainlocation, container, false);

		TurnWebcameOn();

		return view;
	}


	public void TurnWebcameOn() {
		Utilities.getGlobalVariable(getActivity()).isShowed = true;
		Intent intent = new Intent("com.google.zxing.client.android.SCANQR");
		intent.setPackage("com.wiim.nhapnhap");
		intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		startActivityForResult(intent, REQUEST_CODE_QR);

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == REQUEST_CODE_QR) {
			if (resultCode == Activity.RESULT_OK) {
				String tagCode = intent.getStringExtra("SCAN_RESULT").trim();
				Toast.makeText(getActivity(), tagCode, Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getActivity(), "cancel", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onResume() {
		super.onResume();
		if (!Utilities.getGlobalVariable(getActivity()).isShowed) {
			TurnWebcameOn();
		}

	}


}
